package io.sqrtqiezi.practise.hadoop.sort.secondary;

import org.apache.hadoop.io.*;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class DateTemperaturePair implements Writable, WritableComparable<DateTemperaturePair> {
    private Text yearMonth = new Text();
    private Text day = new Text();
    private IntWritable temperature = new IntWritable();

    @Override
    public int compareTo(DateTemperaturePair pair) {
        int compareValue = this.yearMonth.compareTo(pair.getYearMonth());
        // 二次排序
        if (compareValue == 0) {
            compareValue = temperature.compareTo(pair.getTemperature());
        }
        // return compareValue; // 升序排序
        return -1 * compareValue;  // 降序排序
    }

    @Override
    public String toString() {
        return yearMonth.toString() + day.toString() + ":" + temperature.toString();
    }

    public IntWritable getTemperature() {
        return temperature;
    }

    public Text getYearMonth() {
        return yearMonth;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        yearMonth.write(dataOutput);
        day.write(dataOutput);
        temperature.write(dataOutput);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        yearMonth.readFields(dataInput);
        day.readFields(dataInput);
        temperature.readFields(dataInput);
    }

    public void setYearMonth(Text yearMonth) {
        this.yearMonth = yearMonth;
    }

    public Text getDay() {
        return day;
    }

    public void setDay(Text day) {
        this.day = day;
    }

    public void setTemperature(IntWritable temperature) {
        this.temperature = temperature;
    }
}
